using System;
using System.ComponentModel;
using System.IO.Packaging;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using System.IO;
using System.ComponentModel.Design;

namespace OWA.Activities
{
    [ActivityValidator(typeof(AbstractPackageActivityValidator))]
    [Designer(typeof(AbstractPackageActivityDesigner), typeof(IDesigner))]
	public abstract class AbstractPackageActivity 
        : Activity
	{
        static readonly string RT_MainDocument = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
        public static DependencyProperty PackageProperty = 
            DependencyProperty.Register("Package", typeof(Package), typeof(AbstractPackageActivity));

        [Description("The package to work with in the activity.")]
        [Category("OWA")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Package Package
        {
            get
            {
                return ((Package)(base.GetValue(AbstractPackageActivity.PackageProperty)));
            }
            set
            {
                base.SetValue(AbstractPackageActivity.PackageProperty, value);
            }
        }

        protected AbstractPackageActivity()
        {   
        }

        protected Package GetPackage()
        {
            Package package = Package;
            if (package == null)
            {
                throw new InvalidOperationException();
            }
            return package;
        }

        protected PackagePart GetMainPart(Package package)
        {
            PackagePart mainPart = null;
            foreach (PackageRelationship relationship in
                package.GetRelationshipsByType(RT_MainDocument))
            {
                Uri partUri = PackUriHelper.ResolvePartUri(
                    relationship.SourceUri, relationship.TargetUri);
                mainPart = package.GetPart(partUri);
                break;
            }
            if (mainPart == null)
            {
                throw new OWAException("Unexpected missing content in package, missing main part");
            }
            return mainPart;
        }

        protected Uri CreatePartUri(Package package, string uriString)
        {
            string folder = Path.GetDirectoryName(uriString);
            if (folder.StartsWith("/") == false)
            {
                folder = "/" + folder;
            }
            string fileName = Path.GetFileNameWithoutExtension(uriString);
            string extension = Path.GetExtension(uriString);
            string number = "";
            int count = 0;
            do
            {
                string path = Path.Combine(
                    folder, fileName + number + extension);
                Uri uri = new Uri(path, UriKind.Relative);
                if (package.PartExists(uri) == false)
                {
                    return uri;
                }
            }
            while (++count < 1000);
            throw new OWAException("Could not create unique part name");
        }
	}
}
